<?php

namespace App\Module\Api\Controllers;
use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\UserDetailsRequest;
use App\Module\Api\Validator\UserDetailsValidator;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisUserRepository;

/**
 * Class UserDetailsController 用户详细
 * @package App\Module\Admin\Controllers
 */
class UserDetailsController extends Controller
{

    public function __construct()
    {
        $this->query = new UserDetailsRequest();
        parent::__construct();

    }

    public function index()
    {
        (new UserDetailsValidator())->check();
        $user_id = $this->query->userId;
        $masterId = TokenService::getMasterId();
        $sun_user_id = TokenService::getCacheDataByKey('sunUserId');
        $user = CisUserRepository::getInstance();
        if($user_id != $sun_user_id)
        {//提交的用户ID与当前登录的用户ID不一至时需要判断当前帐号是否合法
            $sun_user_list = $user->getChkList($masterId,true);//查所有子帐号
            if(!$sun_user_list)
            {
                throw new ParamsException([
                    'error_code' => '1103',
                    'message'    => '创客不存在',
                ]);
            }

            $sun_user_id_array = [];
            foreach ($sun_user_list as $v)
            {
                $sun_user_id_array[] = $v->id;
            }

            if(!in_array($user_id,$sun_user_id_array))
            {//提交的创客不在主帐号下
                throw new ParamsException([
                    'error_code' => '1103',
                    'message'    => '创客不存在',
                ]);
            }
        }

        $user_info = $user->getUser($user_id);
        if(!$user_info)
        {
            throw new ParamsException([
                'error_code' => '1103',
                'message'    => '创客不存在',
            ]);
        }
        if($user_info->status != 1)
        {
            throw new ParamsException([
                'error_code' => '1106',
                'message'    => '创客帐号被停用，请联系管理开启',
            ]);
        }

        if(!$sun_user_id || $user_id != $sun_user_id)
        {//redis没有缓存写入缓存
            TokenService::getUserId($user_id);//写入redis缓存
        }
        $master_info = $user->getUser($masterId);
        $achievement = $user_info->entrepreneurship_achievement + $user_info->chuangheng_achievement + $user_info->director_achievement;
        $item = [
            'id' => $user_info->id,
            'userType' => $user_info->user_type,
            'name' => $user_info->name,
            'mobile' => $user_info->mobile,
            'idName' => $user_info->id_name,
            'imagePath' => $user_info->head_image_path,
            'integral' => $user_info->integral,
            'freezeIntegral' => $user_info->freeze_integral,
            'registeredCurrency' => $user_info->registered_currency,
            'freezeRegisteredCurrency' => $user_info->freeze_registered_currency,
            'totalUsers' =>$user->getRecommendList($user_info->id,true)->count(),
            'achievement' => number_format($achievement,2,'.',''),
            'isHasPayPassword' => $master_info->pay_password ? 1 : 0,
            'registerDate' => $user_info->created_at->toDateTimeString(),
            'repeatedConsumptionDate' => $user_info->repeated_consumption_date
        ];
        $data['user'] = $item;
        return apiResponse($data);
    }
}
